{% extends "base.html" %}

{% block title %}管理面板 - 墨韵图书共享平台{% endblock %}

{% block content %}
<div class="admin-panel">
    <div class="row mb-4">
        <div class="col-md-12">
            <h2 class="ink-decoration mb-4">管理面板</h2>
            <p class="text-muted">欢迎使用管理系统，您可以在这里管理平台的内容和用户。</p>
        </div>
    </div>
    
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="stat-card">
                <i class="fas fa-users stat-icon"></i>
                <div class="stat-value">{{ user_count }}</div>
                <div class="stat-label">注册用户</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <i class="fas fa-book stat-icon"></i>
                <div class="stat-value">{{ book_count }}</div>
                <div class="stat-label">图书总数</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <i class="fas fa-comment-alt stat-icon"></i>
                <div class="stat-value">{{ post_count }}</div>
                <div class="stat-label">帖子总数</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <i class="fas fa-clock stat-icon"></i>
                <div class="stat-value">{{ pending_books }}</div>
                <div class="stat-label">待审核图书</div>
            </div>
        </div>
    </div>
    
    {% if pending_posts > 0 %}
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="alert alert-warning">
                <i class="fas fa-exclamation-triangle me-2"></i>
                当前有 <strong>{{ pending_posts }}</strong> 篇帖子等待审核，
                <a href="{{ url_for('admin_posts', status='pending') }}" class="alert-link">点击这里进行审核</a>
            </div>
        </div>
    </div>
    {% endif %}

    <div class="row">
        <div class="col-md-3">
            <div class="card ink-card mb-4">
                <div class="card-header admin-title-white">
                    <h5 class="mb-0">管理菜单</h5>
                </div>
                <div class="list-group list-group-flush">
                    <a href="{{ url_for('admin_panel') }}" class="list-group-item list-group-item-action active">
                        <i class="fas fa-tachometer-alt me-2"></i> 仪表盘
                    </a>
                    <a href="{{ url_for('admin_books') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-book me-2"></i> 图书管理
                    </a>
                    <a href="{{ url_for('admin_posts') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-comments me-2"></i> 帖子管理
                        {% if pending_posts > 0 %}
                        <span class="badge bg-danger float-end">{{ pending_posts }}</span>
                        {% endif %}
                    </a>
                    <a href="{{ url_for('admin_users') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-users me-2"></i> 用户管理
                    </a>
                    <a href="{{ url_for('admin_categories') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-tags me-2"></i> 分类管理
                    </a>
                    <a href="{{ url_for('admin_statistics') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-chart-bar me-2"></i> 使用统计
                    </a>
                    <div class="dropdown-divider"></div>
                    <a href="{{ url_for('admin_export_data', type='users') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-file-export me-2"></i> 导出用户数据
                    </a>
                    <a href="{{ url_for('admin_export_data', type='books') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-file-export me-2"></i> 导出图书数据
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-md-9">
            <div class="row">
                <div class="col-md-6">
                    <div class="card ink-card mb-4">
                        <div class="card-header admin-title-white">
                            <h5 class="mb-0">最近活动</h5>
                        </div>
                        <div class="card-body">
                            {% if recent_activities %}
                                <div class="list-group">
                                    {% for activity in recent_activities %}
                                        <div class="list-group-item">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1">{{ activity.user.username }} - {{ activity.activity_type }}</h6>
                                                <small class="text-muted">{{ activity.timestamp.strftime('%m-%d %H:%M') }}</small>
                                            </div>
                                            <p class="mb-1">
                                                {% if activity.target_id %}
                                                    ID: {{ activity.target_id }}
                                                {% else %}
                                                    系统活动
                                                {% endif %}
                                            </p>
                                        </div>
                                    {% endfor %}
                                </div>
                            {% else %}
                                <p class="text-center text-muted">暂无活动记录</p>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="card ink-card mb-4">
                        <div class="card-header admin-title-white">
                            <h5 class="mb-0">活跃用户</h5>
                        </div>
                        <div class="card-body">
                            {% if active_users %}
                                <div class="list-group">
                                    {% for user, count in active_users %}
                                        <div class="list-group-item">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1">{{ user.username }}</h6>
                                                <span class="badge bg-primary">{{ count }} 活动</span>
                                            </div>
                                            <p class="mb-1">
                                                {% if user.is_teacher %}教师{% else %}学生{% endif %}
                                                {% if user.is_admin %}<span class="admin-badge">管理员</span>{% endif %}
                                            </p>
                                        </div>
                                    {% endfor %}
                                </div>
                            {% else %}
                                <p class="text-center text-muted">暂无数据</p>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card ink-card">
                <div class="card-header admin-title-white">
                    <h5 class="mb-0">热门图书</h5>
                </div>
                <div class="card-body">
                    {% if popular_books %}
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>书名</th>
                                        <th>作者</th>
                                        <th>分类</th>
                                        <th>上传者</th>
                                        <th>浏览量</th>
                                        <th>评分</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for book in popular_books %}
                                        <tr>
                                            <td>
                                                <a href="{{ url_for('view_book', book_id=book.id) }}">{{ book.title }}</a>
                                            </td>
                                            <td>{{ book.author }}</td>
                                            <td>
                                                {% if book.category %}
                                                    {{ book.category.name }}
                                                {% else %}
                                                    未分类
                                                {% endif %}
                                            </td>
                                            <td>{{ book.uploader.username }}</td>
                                            <td>{{ book.views }}</td>
                                            <td>
                                                {% if book.rating %}
                                                    {{ "%.1f"|format(book.rating) }}
                                                {% else %}
                                                    暂无评分
                                                {% endif %}
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-center text-muted">暂无热门图书</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 